package com.zzw.mis.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

import java.util.Collections;
import java.util.List;

/**
 * <p>项目名称: zzw-tech </p>
 * <p>包名称: com.zzw.mis.config </p>
 * <p>描述:  </p>
 * <p>  </p>
 * <p>创建时间: 2022/11/24 12 </p>
 *
 * @author coco
 * @version v1.0
 */
@Configuration
@EnableConfigurationProperties({ServerProperties.class})
public class ErrorHandlerConfig {

	@Bean
	@Primary
	@Order(Ordered.HIGHEST_PRECEDENCE)
	public ErrorWebExceptionHandler errorWebExceptionHandler (ObjectProvider<List<ViewResolver>> viewResolversProvider,
	                                                          ServerCodecConfigurer serverCodecConfigurer) {

		JsonExceptionHandler exceptionHandler = new JsonExceptionHandler();
		exceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
		exceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
		exceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
		return exceptionHandler;
	}


}
